package cn.sowz.test;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo10 {
    /*
        整合Activiti
        业务流程建模
        部署业务流程
        启动流程实例
        查询待办任务
        处理待办任务
        结束流程
    * */
    private ProcessEngine processEngine;
    @Before
    public void before() throws Exception {
        // 启动引擎
        this.processEngine = ProcessEngines.getDefaultProcessEngine();
    }

    // 流程部署
    @Test
    public void testDeploy(){
        // 部署流程文件-资源操作-RepositoryService
        RepositoryService repositoryService = this.processEngine.getRepositoryService();
        // 加载流程文件+流程图片,并实现部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("bpmn/leave-candidate.bpmn")
                .name("员工请假流程-candidate")
                .deploy();
        // 输出部署的一些信息
        System.out.println("流程部署ID:"+deploy.getId());
        System.out.println("流程部署名称:"+deploy.getName());
    }

    @Test
    public void testStartProcess(){
        // 启动流程实例--涉及流程运行/执行--RuntimeService
        RuntimeService runtimeService = this.processEngine.getRuntimeService();
        Map<String,Object> map = new HashMap<>();
        map.put("candidate","zhangsan,lisi");

        // 流程实例对象--映射到act_hi_procinst
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("leave-candidate", map);

        // 获取流程实例的相关信息s
        System.out.println("流程定义的ID:"+instance.getProcessDefinitionId());
        System.out.println("流程实例的ID:"+instance.getId());
    }

    // 查询某个待办任务
    // 查询某个候选任务
    @Test
    public void testSelectCandidateTaskList(){
        // 获取TaskService
        TaskService taskService = this.processEngine.getTaskService();
        // 获取任务集合
        List<Task> taskList = taskService.createTaskQuery()
                .processDefinitionKey("leave-candidate")
                // .taskAssignee()
                .taskCandidateUser("zhangsan")
                .list();
        // 遍历任务列表
        for (Task task : taskList) {
            System.out.println("流程定义id = " + task.getProcessDefinitionId());
            System.out.println("流程实例id = " + task.getProcessInstanceId());
            System.out.println("任务id = " + task.getId());
            System.out.println("任务名称 = " + task.getName());
        }
    }

    // 候选人领取任务
    @Test
    public void testClaimTask(){
        TaskService taskService = this.processEngine.getTaskService();
        taskService.claim("2506","zhangsan");
    }

    // 候选人放弃任务
    @Test
    public void testClaimTask2(){
        TaskService taskService = this.processEngine.getTaskService();
        taskService.claim("2506",null);
    }

    @Test
    public void testCompleteTask(){
        // 启动activiti7流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 处理任务--涉及任务处理(task)--TaskService
        TaskService taskService = processEngine.getTaskService();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        List<Task> taskList = taskService.createTaskQuery()
                .processDefinitionKey("leave-candidate") // 查询条件：流程定义的kev--区分不同审批流程
                .taskAssignee("zhangsan") // 查询条件： 节点（任务）审核人（负责人）
                .list();

        // 处理
        for (Task task : taskList) {
            // 先获取流程实例绑定的businessKey
            // task 对象没有businessKey属性,需要通过processInstanceId属性间接查询流程实例对象获取
            String processInstanceId = task.getProcessInstanceId();
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .singleResult();
            String businessKey = processInstance.getBusinessKey(); // 假装查询请假单信息
            System.out.println(businessKey);
            taskService.complete(task.getId());
        }
    }
}
